package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ArrayInfo;
import com.admission.model.Environment;
import com.admission.model.EnvironmentPicture;
import com.admission.service.EnvironmentManager;
import com.admission.service.EnvironmentPictureManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class EnvironmentAdminAction extends CrudActionSupport<Environment>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File[] subPicture;
	private String[] subPictureFileName;//图片名
	private String[] title;
	private List<EnvironmentPicture> pictures;
	private String id;
	private String ids;// 逗号隔开id集合
	private EnvironmentManager environmentManager;
	private EnvironmentPictureManager environmentPictureManager;
	private Environment entity;
	private Page<Environment> page=new Page<Environment>(PageUtils.DEFAULT_PAGE_SIZE);
	private List<Environment> environmentList;
	private String value;
	private String status;
	private List<ArrayInfo> arrayList;
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Environment getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		environmentList=environmentManager.getPageByValue(value);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		arrayList=environmentManager.getByBigType(value);
		return INPUT;
	}
	
	/**
	 * 保存
	 */
	@Override
	public String save() throws Exception {
			String serverrealPath = ServletActionContext.getServletContext()
								.getRealPath("/upload");
			File dir = new File(serverrealPath);
				if (!dir.exists()) {
					dir.mkdir();
				}
					String[] subPath = null;
					try{
						if (subPicture != null) {
							subPath = new String[subPicture.length];
							for (int i = 0; i < subPicture.length; i++) {
									subPath[i] = NewFileName.genNewFileName(subPictureFileName[i],i);
									FileUtils.copyFile(subPicture[i], new File(serverrealPath,subPath[i]));
							}
						}
							environmentManager.save(entity);
							environmentPictureManager.save(entity.getId(),subPath,title);
							// this.addActionMessage("操作成功");
						} catch (Exception e) {
						   this.addActionMessage("操作失败，请重新操作");
					    }	
						return list();
			}
	
	/**
	 * 检查类型重复
	 * @return
	 */
	public String checkType() throws Exception {
			if(environmentManager.getByType(entity.getType())==null)
				Struts2Utils.renderText("true");
			else
				Struts2Utils.renderText("false");
			return null;
	}
	
	
	
	
	
	/**
	 * 批量删除
	 */
	@Override
	public String delete() throws Exception {
		try{
			environmentManager.deleteByIds(ids);
			 //this.addActionMessage("操作成功");
	   } catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	   }  
		return list();
	}
	
	
	/**
	 * 删除编辑时单张图片
	 * @return
	 * @throws Exception
	 */
	public String deleteById() throws Exception{
		environmentPictureManager.deleteById(ids);
		entity=environmentManager.getById(id);
		pictures=environmentPictureManager.getSubPicture(entity.getId());
		return INPUT;
	}
	
	
	/**
	 * 改变开放状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		environmentManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity=new Environment();
		}
		else
			{
			entity=environmentManager.getById(id);
			pictures=environmentPictureManager.getSubPicture(entity.getId());
			}
		
	}

	public File[] getSubPicture() {
		return subPicture;
	}

	public void setSubPicture(File[] subPicture) {
		this.subPicture = subPicture;
	}

	public String[] getSubPictureFileName() {
		return subPictureFileName;
	}

	public void setSubPictureFileName(String[] subPictureFileName) {
		this.subPictureFileName = subPictureFileName;
	}

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}
	@Autowired
	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}

	public EnvironmentPictureManager getEnvironmentPictureManager() {
		return environmentPictureManager;
	}
	@Autowired
	public void setEnvironmentPictureManager(
			EnvironmentPictureManager environmentPictureManager) {
		this.environmentPictureManager = environmentPictureManager;
	}

	public Environment getEntity() {
		return entity;
	}

	public void setEntity(Environment entity) {
		this.entity = entity;
	}

	public List<EnvironmentPicture> getPictures() {
		return pictures;
	}

	public void setPictures(List<EnvironmentPicture> pictures) {
		this.pictures = pictures;
	}


	public Page<Environment> getPage() {
		return page;
	}

	public void setPage(Page<Environment> page) {
		this.page = page;
	}

	public List<Environment> getEnvironmentList() {
		return environmentList;
	}

	public void setEnvironmentList(List<Environment> environmentList) {
		this.environmentList = environmentList;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String[] getTitle() {
		return title;
	}

	public void setTitle(String[] title) {
		this.title = title;
	}

	public List<ArrayInfo> getArrayList() {
		return arrayList;
	}

	public void setArrayList(List<ArrayInfo> arrayList) {
		this.arrayList = arrayList;
	}


}
